#!/bin/env python
#coding=utf-8
#*/1 * * * * root /opt/fonsview/3RD/node_exporter/textfile_scripts/runner node_ats_corefile
#ats core文件数监控

import subprocess
from os import listdir
from os.path import isfile,isdir, join

ne_path = "/opt/fonsview/NE"
cache_flag =("CSX","CSD")

metrics = """
# HELP node_ats_corefile core
# TYPE node_ats_corefile gauge
node_ats_corefile {VALUE}
"""
def main():
    core_sum = 0
    cache = ""
    for _flag in cache_flag:
        ats_dir = join(ne_path,_flag.lower(),'trafficserver')
        if isdir(ats_dir):
            cache = _flag.upper()
            onlyfiles = [f for f in listdir(ats_dir) if isfile(join(ats_dir, f))]
            for f_name in onlyfiles:
                # print(f_name)
                if f_name.startswith("core"):
                    core_sum = core_sum + 1
    if cache:
        print(metrics.format(VALUE=core_sum))
    

if __name__ == '__main__':
    main()